package com.itheima.controller;

import com.itheima.entity.PageDTO;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.utils.AliyunOssUitls;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author wa1nut
 */
@RestController
@Slf4j
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @Autowired
    private AliyunOssUitls aliyunOssUitls;

    @PostMapping("/page")
    public PageResult<Brand> page(PageDTO pageDTO,@RequestBody Brand brand){
        log.info("{}",brandService.page(pageDTO,brand));
        return brandService.page(pageDTO,brand);
    }

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        return new Result<>(true,20000,"执行成功",aliyunOssUitls.uploadImg(file));
    }

    @PostMapping("/add")
    public Result<Object> add(@RequestBody Brand brand){
        brandService.add(brand);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result<Object> delete(@PathVariable Integer id){
        brandService.delete(id);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result<Object> findById(@PathVariable Integer id){
        return new Result<>(true,20000,"执行成功",brandService.findById(id));
    }

    @PutMapping("/update")
    public Result<Object> update(@RequestBody Brand brand){
        brandService.update(brand);
        return Result.success();
    }



}
